// pages/travel/travel.js
var lo = 120.577315,
  la = 30.029130
var dlo, dla, that, isSubmit = false
Page({
  data: {
    longitude: 120.577315,
    latitude: 30.029130,
    clongitude: 120.577315,
    clatitude: 30.029130,
    scale: 16,
    index: 0,
    result: "先提交坐标，再完成挑战",
    points: [{
      name: "北京：天安门城楼",
      longitude: 116.397506,
      latitude: 39.908878
    }, {
      name: "上海：东方明珠塔",
      longitude: 121.499711,
      latitude: 31.239808
    }, {
      name: "杭州：雷峰塔",
      longitude: 120.148868,
      latitude: 30.231746
    }, {
      name: "广州：广州塔",
      longitude: 113.324445,
      latitude: 23.106459
    }, {
      name: "拉萨：布达拉宫",
      longitude: 91.118281,
      latitude: 29.654947
    }],
    pointList: [
      "北京：天安门城楼",
      "上海：东方明珠塔",
      "杭州：雷峰塔",
      "广州：广州塔",
      "拉萨：布达拉宫"
    ]
  },
  pickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  onReady: function (e) {
    that = this
    this.mapCtx = wx.createMapContext('myMap')
  },
  regionChange(e) {
    if (e.type == 'end') {
      console.log(e)
      const centerLocation = e.detail.centerLocation
      this.setData({
        clongitude: Math.round(centerLocation.longitude * 1000000) / 1000000.0,
        clatitude: Math.round(centerLocation.latitude * 1000000) / 1000000.0
      })
    }
  },
  getCenter: function () {
    isSubmit = true
    this.mapCtx.getCenterLocation({
      success: function (res) {
        lo = Math.round(res.longitude * 1000000) / 1000000.0
        la = Math.round(res.latitude * 1000000) / 1000000.0
        that.setData({
          longitude: lo,
          latitude: la
        })
        console.log(lo)
        console.log(la)
      }
    })
  },
  compareTo: function () {
    dlo = Number(this.data.points[this.data.index].longitude)
    dla = Number(this.data.points[this.data.index].latitude)
    console.log("dlo" + (dlo - lo))
    console.log("dla" + (dla - la))
    if (!isSubmit) {
      this.setData({
        result: "请先提交坐标。"
      })
    } else if (Math.abs(dlo - lo) < 0.002 && Math.abs(dla - la) < 0.002) {
      this.setData({
        result: "恭喜你！！挑战成功！！"
      })
    } else {
      this.setData({
        result: "挑战失败，请继续努力…"
      })
    }
    isSubmit = false
  },
  zoomIn: function () {
    var s = this.data.scale
    if (s < 20) {
      s++
      this.setData({
        scale: s
      })
      console.log(s)
    }
  },
  zoomOut: function () {
    var s = this.data.scale
    if (s > 3) {
      s--
      this.setData({
        scale: s
      })
    }
  },
  transmit: function () {
    this.setData({
      longitude: this.data.points[this.data.index].longitude,
      latitude: this.data.points[this.data.index].latitude,
      scale: 18
    })
  }
})